package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.List;

/**
 * 2023/1/16 16:51 星期一<br/>
 * <p>
 * 选中时，三态选择控件通常外观为带有复选标记或刻度标记的框
 *
 * @author xuMingHai
 */
public class CheckBoxApp extends Application {

    private final AnchorPane anchorPane = new AnchorPane();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() {
        CheckBox checkBox = new CheckBox("CheckBox");
        // 是否循环未定义状态
        checkBox.setAllowIndeterminate(true);
        checkBox.setOnAction(event -> {
            System.out.println("checkBox.isSelected() = " + checkBox.isSelected());
            System.out.println("checkBox.isIndeterminate() = " + checkBox.isIndeterminate());
        });


        // 设置菜单
        // checkMenuItem 只有选定和未选定状态
        CheckMenuItem checkMenuItem = new CheckMenuItem("CheckMenuItem");
        // 双向绑定checkBox
        checkMenuItem.selectedProperty().bindBidirectional(checkBox.selectedProperty());
        checkBox.setContextMenu(new ContextMenu(checkMenuItem));


        // create the tree model
        CheckBoxTreeItem<String> jonathanGiles = new CheckBoxTreeItem<>("Jonathan");
        CheckBoxTreeItem<String> juliaGiles = new CheckBoxTreeItem<>("Julia");
        CheckBoxTreeItem<String> mattGiles = new CheckBoxTreeItem<>("Matt");
        CheckBoxTreeItem<String> sueGiles = new CheckBoxTreeItem<>("Sue");
        CheckBoxTreeItem<String> ianGiles = new CheckBoxTreeItem<>("Ian");

        CheckBoxTreeItem<String> gilesFamily = new CheckBoxTreeItem<>("Giles Family");
        gilesFamily.setExpanded(true);
        // TODO: 可变参数与泛型，泛型会转为Object[]，编译器会发出警告，javafx 未添加 @SafeVarargs
        gilesFamily.getChildren().addAll(List.of(jonathanGiles, juliaGiles, mattGiles, sueGiles, ianGiles));

        // create the treeView
        final TreeView<String> treeView = new TreeView<>();
        treeView.setRoot(gilesFamily);

        // set the cell factory
        treeView.setCellFactory(CheckBoxTreeCell.forTreeView());

        anchorPane.getChildren().addAll(checkBox, treeView);

        AnchorPane.setRightAnchor(treeView, 0.0);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        primaryStage.setScene(new Scene(anchorPane, 800, 600));
        primaryStage.show();
    }
}
